package com.example.youxiang;

import org.springframework.scheduling.annotation.Scheduled;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author:郭
 * @Description: TODO
 * @Date: 2022/12/19 13:25
 * @ClassName Time
 */
public class SendSystemStatus {
    static EmailUtil emailUtil = new EmailUtil();
    @Scheduled(fixedRate = 30000)
    public void sendMaile(){
        double cpuUseage = getCpuUseage();
        double menUsage = getMenUsage();
        double deskUsage = getDeskUsage();
        double projectUsage = getProjectUsage();
        if (projectUsage > 80.0){
            emailUtil.sendMessage("1045295788@qq.com","警告","内存使用超过已达"+projectUsage);
        }
        if (cpuUseage > 10.0){
            emailUtil.sendMessage("1045295788@qq.com","警告","CPU使用率已达"+cpuUseage);
        }
        if (menUsage > 80.0){
            emailUtil.sendMessage("1045295788@qq.com","警告","内存使用率已达"+menUsage);
        }
        if (deskUsage > 80.0){
            emailUtil.sendMessage("1045295788@qq.com","警告","磁盘使用率已达"+deskUsage);
        }
    }
    public double getCpuUseage(){
        double cpuUsed = 0;
        Runtime rt = Runtime.getRuntime();
        Process p = null;
        BufferedReader in = null;
        try {
            //调用系统的top命令
            p = rt.exec("top -b -n 1");
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String str = null;
            String[] strArray = null;
            int m = 0;
            while ((str = in.readLine()) != null){
                //只分析正在运行的进程，top本身进程除外
                if (str.indexOf("R") != -1){
                    strArray = str.split(" ");
                    for (String tmp : strArray) {
                        if (tmp.trim().length() == 0)
                            continue;
                        if (++m == 9){
                            cpuUsed += Double.parseDouble(tmp);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return cpuUsed;
    }
    public double getMenUsage(){
        double menUsed = 0;
        Runtime rt = Runtime.getRuntime();
        Process p = null;
        BufferedReader in = null;
        try {
            //调用系统的top命令
            p = rt.exec("top -b -n 1");
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String str = null;
            String[] strArray = null;
            int m = 0;
            while ((str = in.readLine()) != null){
                //只分析正在运行的进程，top本身进程除外
                if (str.indexOf("R") != -1){
                    strArray = str.split(" ");
                    for (String tmp : strArray) {
                        if (tmp.trim().length() == 0)
                            continue;
                        if (++m == 9){
                            menUsed += Double.parseDouble(tmp);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return menUsed;
    }
    public double getDeskUsage(){
        double totalHD = 0;
        double usedHD = 0;
        Runtime rt = Runtime.getRuntime();
        Process p = null;
        BufferedReader in = null;
        try {
            p = rt.exec("df -h1 /home");
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String str = null;
            String[] strArray = null;
            while ((str = in.readLine()) != null){
                int m = 0;
                strArray = str.split(" ");
                for (String tmp : strArray) {
                    if (tmp.trim().length() == 0)
                        continue;
                    ++m;
                    if (tmp.indexOf("G") != -1){
                        if (m == 2){
                            System.out.println(tmp);
                            if (!tmp.equals("") && !tmp.equals("0"))
                                usedHD += Double.parseDouble(tmp.substring(0,tmp.length() - 1)) * 1024;
                        }
                        if (m == 3){
                            System.out.println(tmp);
                            if (!tmp.equals("none") && !tmp.equals("0"))
                                totalHD += Double.parseDouble(tmp.substring(0,tmp.length() - 1)) * 1024;
                        }
                    }
                    if (tmp.indexOf("M") != -1){
                        if (m == 2){
                            if (!tmp.equals("") && !tmp.equals("0"))
                                usedHD += Double.parseDouble(tmp.substring(0,tmp.length() - 1));
                        }
                        if (m == 3){
                            if (!tmp.equals("none") && !tmp.equals("0"))
                                totalHD += Double.parseDouble(tmp.substring(0,tmp.length() -1));
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return (usedHD / totalHD) * 100;
    }
    public double getProjectUsage(){
        double projectUsage = 0;
        Runtime run = Runtime.getRuntime();
        long max = run.maxMemory();
        long total = run.totalMemory();
        long free = run.freeMemory();
        long usable = max - total + free;
        projectUsage = 100 - (usable / max) * 100;
        return projectUsage;
    }
}
